﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppAuthAdmin.Code;
using WebAppAuthAdmin.AuthApiManagementServiceReference;
using Web.Framework;
using AuthApi.Contracts;
using WebAppAuthAdmin.Code.Security;

namespace WebAppAuthAdmin.App
{
    public partial class CompanyList : BasePage
    {
        List<Company> companyList = new List<Company>();      

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {                
                this.lnkBuscar.Attributes.Add("aspnetid", this.lnkBuscar.UniqueID);
                if (!this.IsPostBack)
                {
                    Buscar();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected override void ApplySecurity()
        {
            this.GridCompany.Columns[1].Visible = HasPermissionFor(AppSecurity.APP_DELETE_COMPANY);
        }

        private void Buscar()
        {
            companyList = WebServiceHelper.GetCompanyList(this.txtNombre.Text);
            this.GridCompany.DataSource = companyList;
            this.GridCompany.DataBind();
        }       

        protected void GridCompany_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "editar")
                {
                    int companyId = Convert.ToInt32(this.GridCompany.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    this.Response.Redirect(string.Format("/App/CompanyEdit.aspx?Id={0}", companyId), true);
                }
                if (e.CommandName == "eliminar")
                {
                    int companyId = Convert.ToInt32(this.GridCompany.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    WebServiceHelper.DeleteCompanyById(companyId);
                    Buscar();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridCompany_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Company company = (Company)e.Row.DataItem;
                    if (!HasPermissionFor(AppSecurity.APP_VIEW_COMPANY_ID_ + company.Id.ToString()))
                    {
                        e.Row.Visible = false;
                    }

                    if (!HasPermissionFor(AppSecurity.APP_EDIT_COMPANY_ID_ + company.Id.ToString()))
                    {
                        e.Row.Cells[0].Text = string.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void btnAgregarNuevo_Click(object sender, EventArgs e)
        {
            try
            {
                this.Response.Redirect("/App/CompanyEdit.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
        
    }
}